﻿using nanhua.BookStore.DomainServices;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Auditing;
using Volo.Abp.Domain.Entities;

namespace nanhua.BookStore.Entitys.OrderAggregate
{
    public class Order : AggregateRoot<Guid>, IHasCreationTime
    {
        /// <summary>
        /// 总价价钱
        /// </summary>
        private decimal _totalPrice;
        public virtual decimal TotalPrice { 
            get { 
                return GetTotalPrice(); 
            }
            private set {
                _totalPrice = value;
            }
        }
        /// <summary>
        /// 支付类型
        /// </summary>
        public virtual PayTypeEnum PayType { get; private set; }
        /// <summary>
        /// 订单状态
        /// </summary>
        public virtual OrderStatusEnum OrderStatus { get; private set; }
        /// <summary>
        /// 支付时间
        /// </summary>
        public virtual DateTime PayTime { get; private set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public virtual DateTime CreationTime { get; private set; }
        /// <summary>
        /// 订单详情
        /// </summary>
        public virtual Collection<OrderDetail> OrderDetails { get; private set; }

        public Order(Guid id):base(id) 
        {
            this.CreationTime = DateTime.Now;
            this.OrderDetails = new Collection<OrderDetail>();
        }

        public virtual void AddOrderDetail(OrderDetail orderDetail)
        {
            this.OrderDetails.Add(orderDetail);
        }
        public virtual void RemoveOrderDetail(Guid OrderDetailId)
        {
            this.OrderDetails.RemoveAll(c => c.Id == OrderDetailId);
        }

        public decimal GetTotalPrice()
        {
            decimal totalPrice = 0;
            foreach (var item in OrderDetails)
            {
                totalPrice += item.Price;
            }
            return totalPrice;
        }

        /// <summary>
        /// 付款
        /// </summary>
        /// <param name="payType"></param>
        public void Pay(PayTypeEnum payType)
        {
            this.PayType = payType;
            this.OrderStatus = OrderStatusEnum.Payed;
            this.PayTime = DateTime.Now;
        }
    }
}
